//
//  CommentTableViewCell.m
//  CYWFrame
//
//  Created by Macx on 2017/8/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "CommentTableViewCell.h"
#import "Comment.h"
#import "ArticleComment.h"

#import "NSString+Common.h"

#import "PushPerHome.h"

@implementation CommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    self.nickName.font = kUHSystemFontWithSize(12);
    self.mood.font = kUHSystemFontWithSize(12);
    self.mood.numberOfLines = 0;
    self.mood.lineBreakMode = NSLineBreakByWordWrapping;
    self.date.font = kUHSystemFontWithSize(10);
    
    self.headPic.frame = CGRectMake(10, 10, 35, 35);
    self.headPic.layer.cornerRadius = 17.5;
    self.headPic.layer.masksToBounds = YES;
    
    self.level.frame = CGRectMake(self.headPic.x+self.headPic.width/3*2, self.headPic.y+self.headPic.height/3*2, self.headPic.width/3, self.headPic.width/3);
    
    
    CGSize size2 = [LZCTools textSizeWithText:@"5小时之前" font:kUHSystemFontWithSize(10)];
    self.date.frame = CGRectMake(self.headPic.x+self.headPic.width+10, self.headPic.y+self.headPic.height-size2.height, SW(2), size2.height);
    CGSize size3 = [LZCTools textSizeWithText:@"未关注" font:kUHSystemFontWithSize(12)];
    
    [self.comment setImage:[IMAGE(@"icon_comment") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    [self.comment setTitle:@" 评论" forState:UIControlStateNormal];
    self.comment.titleLabel.font = kUHSystemFontWithSize(13);
    self.comment.frame = CGRectMake(SCREEN_WIDTH-self.headPic.width, 10, self.headPic.width, self.headPic.height);
    
    
    [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.praise setTitle:@" 99" forState:UIControlStateNormal];
    self.praise.titleLabel.font = kUHSystemFontWithSize(12);
    self.praise.frame = CGRectMake(SCREEN_WIDTH-self.headPic.width*2, 10, self.headPic.width, self.headPic.height);
    
    
    self.deleteBtn.titleLabel.font = kUHSystemFontWithSize(12);
    self.deleteBtn.frame = CGRectMake(SCREEN_WIDTH-self.headPic.width*3, 10, self.headPic.width, self.headPic.height);
    self.deleteBtn.hidden = YES;
    
    self.subReplyArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        UILabel *reply = [[UILabel alloc] init];
        reply.numberOfLines = 0;
        reply.clipsToBounds = YES;
        reply.font = kUHSystemFontWithSize(12);
        reply.textColor = kBlackColor;
        
        [self.subReplyArr addObject:reply];
    }
    
}

- (void)setArticleModel:(ArticleComment *)articleModel {
    if (_articleModel != articleModel) {
        _articleModel = articleModel;
        
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[articleModel.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        
        [self.headPic addActionWithblock:^{
            [PushPerHome pushPerHomeWithUserInfo:[articleModel valueForKey:@"author"]];
        }];

        
        self.nickName.text = [articleModel.author valueForKey:@"nickname"];
        CGSize size1 = [LZCTools textSizeWithText:self.nickName.text font:kUHSystemFontWithSize(12)];
        self.nickName.frame = CGRectMake(self.headPic.x+self.headPic.width+10, 10, size1.width, size1.height);
        
        if ([[articleModel.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.level.image = IMAGE(@"");
        }
        else if ([[articleModel.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.level.image = IMAGE(@"icon_add_v");
        }
        else if ([[articleModel.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.level.image = IMAGE(@"icon_add_v_expert");
        }
        
        
        
        NSString *time = [NSString timeWithTimeIntervalString:[NSString stringWithFormat:@" %ld", (long)articleModel.create_time]];
        self.date.text = time;
        
        if ([[articleModel valueForKey:@"is_zan"] isEqual:@1]) {
            [self.praise setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else
            [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [self.praise setTitle:[NSString stringWithFormat:@" %ld", articleModel.zans_count] forState:UIControlStateNormal];
        
        self.mood.text = articleModel.content;
        CGSize size2 = [LZCTools textSizeWithText:self.mood.text font:self.mood.font];
        CGSize moodSize = [self.mood.text sizeWithFont:kUHSystemFontWithSize(13) constrainedToSize:CGSizeMake(SCREEN_WIDTH-self.headPic.x*2, size2.height*1000) lineBreakMode:NSLineBreakByWordWrapping];
        self.mood.frame = CGRectMake(self.nickName.x, self.headPic.y+self.headPic.height+10, SCREEN_WIDTH-self.nickName.x-10, moodSize.height);
        
        self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, 0);
        
        [self.againView removeAllSubViews];
        
        if (articleModel.children.count > 0) {
            
            self.againView.hidden = NO;
            
            CGFloat replyH1 = 0.0;
            CGFloat replyH2 = 0.0;
            //            NSMutableArray *hArr = [[NSMutableArray alloc] init];
            
            for (int i = 0; i < articleModel.children.count; i++) {
                
//                UILabel *reply = [[UILabel alloc] init];
//                [self.againView addSubview:reply];
//                reply.numberOfLines = 0;
//                reply.font = kUHSystemFontWithSize(12);
                //                reply.textColor = kGrayColor;
                UILabel *reply = self.subReplyArr[i];
                [self.againView addSubview:reply];
                
                
                if (i == 2) {
                    reply.text = @"查看全部回复";
                    reply.textColor = RGB(70, 102, 165);
                    reply.frame = CGRectMake(SW(40), 30+replyH1+replyH2, self.againView.width-SW(20), size1.height);
                    
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+replyH2+size1.height+40);
                    
                    break;
                }
                
                NSString *nick = [[articleModel.children[i] valueForKey:@"author"] valueForKey:@"nickname"];
                NSString *content = [articleModel.children[i] valueForKey:@"content"];
                reply.text = [NSString stringWithFormat:@"%@:%@", nick, content];
                
                // 不同颜色
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:reply.text];
                NSRange redRange = NSMakeRange([[string string] rangeOfString:[NSString stringWithFormat:@"%@", nick]].location, [[string string] rangeOfString:[NSString stringWithFormat:@"%@", nick]].length);
                [string addAttribute:NSForegroundColorAttributeName value:RGB(70, 102, 165) range:redRange];
                [reply setAttributedText:string];
                
                CGSize size = [LZCTools textSizeWithText:reply.text font:reply.font];
                CGSize replySize = [reply.text sizeWithFont:kUHSystemFontWithSize(12) constrainedToSize:CGSizeMake(SCREEN_WIDTH-10-self.nickName.x, size.height*1000) lineBreakMode:NSLineBreakByWordWrapping];
                
                if (i == 0) {
                    reply.frame = CGRectMake(SW(40), 10, self.againView.width-SW(20), replySize.height);
                    replyH1 = replySize.height;
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+20);
                }
                else if (i == 1) {
                    reply.frame = CGRectMake(SW(40), 20+replyH1, self.againView.width-SW(20), replySize.height);
                    replyH2 = replySize.height;
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+replyH2+30);
                }
                
            }
        }
        else {
            self.againView.frame = CGRectMake(0, 0, 0, 0);
            self.againView.hidden = YES;
        }
        [self updateConstraints];
    }
}

- (void)setModel:(Comment *)model {
    
    if (_model != model) {
        _model = model;
        
        [self.headPic sd_setImageWithURL:[NSURL URLWithString:[model.author valueForKey:@"face"]] placeholderImage:IMAGE(@"placeHolder")];
        [self.headPic addActionWithblock:^{
            [PushPerHome pushPerHomeWithUserInfo:[model valueForKey:@"author"]];
        }];
        
        self.nickName.text = [model.author valueForKey:@"nickname"];
        CGSize size1 = [LZCTools textSizeWithText:self.nickName.text font:kUHSystemFontWithSize(12)];
        self.nickName.frame = CGRectMake(self.headPic.x+self.headPic.width+10, 10, size1.width, size1.height);
        
        NSString *time = [NSString timeWithTimeIntervalString:[NSString stringWithFormat:@" %ld", (long)model.create_time]];
        self.date.text = time;
        
        if ([[model.author valueForKey:@"role"] isEqualToString:@"1"]) {
            self.level.image = IMAGE(@"");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"2"]) {
            self.level.image = IMAGE(@"icon_add_v");
        }
        else if ([[model.author valueForKey:@"role"] isEqualToString:@"3"]) {
            self.level.image = IMAGE(@"icon_add_v_expert");
        }
        
        if ([[model valueForKey:@"is_zan"] isEqual:@1]) {
            [self.praise setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        }
        else
            [self.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        
        [self.praise setTitle:[NSString stringWithFormat:@" %ld", model.zan_num] forState:UIControlStateNormal];
    
        self.mood.text = model.contents;
        CGSize size2 = [LZCTools textSizeWithText:self.mood.text font:self.mood.font];
        CGSize moodSize = [self.mood.text sizeWithFont:kUHSystemFontWithSize(13) constrainedToSize:CGSizeMake(SCREEN_WIDTH-self.headPic.x*2, size2.height*1000) lineBreakMode:NSLineBreakByWordWrapping];
        self.mood.frame = CGRectMake(self.nickName.x, self.headPic.y+self.headPic.height+10, SCREEN_WIDTH-self.nickName.x-10, moodSize.height);
    
        self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, 0);
        
        [self.againView removeAllSubViews];
        
        if (model.replies.count > 0) {
            
            self.againView.hidden = NO;
            
            CGFloat replyH1 = 0.0;
            CGFloat replyH2 = 0.0;
            
            for (int i = 0; i < model.replies.count; i++) {
                
                UILabel *reply = self.subReplyArr[i];
                [self.againView addSubview:reply];
                
                
                if (i == 2) {
                    reply.text = @"查看全部回复";
                    reply.textColor = RGB(70, 102, 165);
                    reply.frame = CGRectMake(SW(40), 30+replyH1+replyH2, self.againView.width-SW(20), size1.height);
                    
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+replyH2+size1.height+40);
                    
                    break;
                }
                
                NSString *nick = [[model.replies[i] valueForKey:@"author"] valueForKey:@"nickname"];
                NSString *content = [model.replies[i] valueForKey:@"contents"];
                reply.text = [NSString stringWithFormat:@"%@:%@", nick, content];
                
                // 不同颜色
                NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:reply.text];
                NSRange redRange = NSMakeRange([[string string] rangeOfString:[NSString stringWithFormat:@"%@", nick]].location, [[string string] rangeOfString:[NSString stringWithFormat:@"%@", nick]].length);
                [string addAttribute:NSForegroundColorAttributeName value:RGB(70, 102, 165) range:redRange];
                [reply setAttributedText:string];
                
                CGSize size = [LZCTools textSizeWithText:reply.text font:reply.font];
                CGSize replySize = [reply.text sizeWithFont:kUHSystemFontWithSize(12) constrainedToSize:CGSizeMake(SCREEN_WIDTH-10-self.nickName.x, size.height*1000) lineBreakMode:NSLineBreakByWordWrapping];
                
                if (i == 0) {
                    reply.frame = CGRectMake(SW(40), 10, self.againView.width-SW(20), replySize.height);
                    replyH1 = replySize.height;
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+20);
                }
                else if (i == 1) {
                    reply.frame = CGRectMake(SW(40), 20+replyH1, self.againView.width-SW(20), replySize.height);
                    replyH2 = replySize.height;
                    self.againView.frame = CGRectMake(self.nickName.x, self.mood.y+self.mood.height+10, SCREEN_WIDTH-self.nickName.x-10, replyH1+replyH2+30);
                }
                
            }
        }
        else {
            self.againView.frame = CGRectMake(0, 0, 0, 0);
            self.againView.hidden = YES;
            [self.againView removeAllSubViews];
        }
        
        [self updateConstraints];
    }
    
}


- (CGFloat)cellHeight {
    
    if (self.againView.height == 0) {
        return self.mood.y+self.mood.height+10;
    }
    else
        return self.againView.height+self.againView.y+10;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
